package com.design.structural.Bridge.Thermometer.Weather_API;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * 描述: 通过runtime执行python代码，从缓冲流中取得结果
 *
 * @author YuJunJie
 * @create 2019-04-04 10:59
 */
public class globalAPI implements weatherAPI{
    @Override
    public String getWeatherInfo(String cityName) {
        String result="";
        try {
            String[] strs=new String[] {"E:\\PythonProject\\crawler\\venv\\Scripts\\python",
                    System.getProperty("user.dir")+"\\src\\com\\design\\structural\\Bridge\\Thermometer\\get_weather_by_api.py", "global", cityName};
            Process pr = Runtime.getRuntime().exec(strs);

            BufferedReader in;
            in = new BufferedReader(new InputStreamReader(pr.getInputStream(),"GBK"));
            String line="";
            while ((line=in.readLine())!=null)
                result += line + "\n";
            in.close();
            pr.waitFor();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
